
clear all
set more off
set maxvar 20000
 
cd /data2/cedsei/firm/

/* Inputs */ 

local in_evana   "/data2/cedsei/firm/event_panel_v2_20230608.dta"

/* Outputs */

********************************************************************************

clear 
use if monthnot>0 using `in_evana'

gen sni2=int(bransch_sni2007/1000)
gen sni3=int(bransch_sni2007/100)
gen q_not=ceil(monthnot/3)
drop if sni2==0

egen qy=group(nyear q_not) 
egen my=group(nyear monthnot)

********************************************************************************

gen ln_size  = log(fsize_rams)
gen l_sales  = log(sales_fek)
gen l_prod   = l_sales - ln_size 
gen l_prod_m = l_sales - l_mon

bys x: gen count = _N 

bys x (evyear): gen p_prod = (l_prod[_n-1] + l_prod[_n-2] + l_prod[_n-3])/3 if count>3
replace  	    p_prod = (l_prod[_n-1] + l_prod[_n-2])		 /2 if count==3
tabmiss p_prod if evyear==0
replace 	    p_prod = p_prod[_n+2] if evyear==-2
replace 	    p_prod = p_prod[_n+1] if evyear==-1
tabmiss p_prod if inlist(evyear,-2,-1,0)

bys x (evyear): gen l_prod_1 = l_prod[_n-1]
bys x (evyear): gen l_prod_2 = l_prod[_n-2]
bys x (evyear): gen ch_prod  = l_prod - l_prod_1
bys x (evyear): gen d_prod   = l_prod - p_prod

********************************************************************************

gen m_a   = chi     / s 
gen m_df  = chi_df  / s
gen m_dj  = chi_dj  / s
gen m_djl = chi_djl / s

********************************************************************************

gen chi_df2=s*(m_df)^2
gen chi_df3=s*(m_df)^3
gen chi_df4=s*(m_df)^4
gen chi_dj2=s*(m_dj)^2
gen chi_dj3=s*(m_dj)^3
gen chi_dj4=s*(m_dj)^4
gen chi_djl2=s*(m_djl)^2
gen chi_djl3=s*(m_djl)^3
gen chi_djl4=s*(m_djl)^4

********************************************************************************

gen s_age=s*age
gen s_ten=s*cumten
gen s_age2=s*age^2
gen s_ten2=s*cumten^2

********************************************************************************

foreach var of varlist  m_a m_df m_dj m_djl {
	sort evyear `var'
	quietly egen temp=rank(`var') if evyear==0
	quietly gen temp2=temp-1 if evyear==0
	quietly sum temp2 if evyear==0
	scalar max=r(max)
	quietly gen temp3=temp2/max
	* line below is an ugly hack
	quietly replace temp3=0.000000000001 if temp2==0
	quietly gen r_`var'=.
	forvalues x=1/100 {
		display `x'
		local y = `x'/100
		local z = `x'/100 - 0.01
		quietly replace r_`var'=`x' if temp3 <=`y' & temp3 > `z'
	}
	drop temp temp2 temp3
	egen temp = max(r_`var'), by(x) 
	replace r_`var' = temp if mi(r_`var')
	drop temp 
}

sort s
quietly egen temp=rank(s) if evyear==0
quietly gen temp2=temp-1 if evyear==0
quietly sum temp2 if evyear==0
scalar max=r(max)
quietly gen temp3=temp2/max
* line below is an ugly hack
quietly replace temp3=0.000000000001 if temp2==0
quietly gen r_s=.
forvalues x=1/100 {
 	display `x'
	local y = `x'/100
	local z = `x'/100 - 0.01
	quietly replace r_s=`x' if temp3 <=`y' & temp3 > `z' & evyear==0
}
drop temp temp2 temp3
egen temp = max(r_s), by(x) 
replace r_s = temp if mi(r_s)
drop temp 

foreach var of varlist ch_prod d_prod {
	winsor2 `var', cuts(5 95) replace by(evyear)
}

save C1_anaprep_v2_20230608, replace 














